package hotel;

import hotel.RicercaAvanzata.MyDefaultTableModel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.toedter.calendar.JDateChooser;

public class VerificaDisponibilita extends JFrame implements ActionListener {
	private JDateChooser chooser1,chooser2;
	private Calendar cal1,cal2;
	private JPanel mainPanel,pannelloSup,pannelloInf;
	private JButton verifica;
	private JComboBox numPersone;
	private String[] persone={"--","1","2","3","4"};
	private JTable tabella;
	private JScrollPane scroll;
	private MyDefaultTableModel modelClienti;
	private MyResultSet rs;
	private GraficaHotel g;
	private JButton prenota;
	private Icon iconae=new ImageIcon("Risorse/iconae.png");
	
	public VerificaDisponibilita(GraficaHotel g){
		this.g=g;
		Image immagineIc = Toolkit.getDefaultToolkit().getImage(
				"Risorse/pollice1.png");
		this.setIconImage(immagineIc);
		this.setSize(850,700);
		this.setResizable(false);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setLocation(250,20);
		this.setTitle("Verifica Disponibilita'");
		mainPanel=new JPanel(new BorderLayout());
		pannelloSup=new JPanel();
		chooser1=new JDateChooser();
		chooser1.setDateFormatString("dd/MM/yyyy");
		chooser1.addPropertyChangeListener(new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent e) {
				cal1 = chooser1.getCalendar();
			}
		});
		chooser2=new JDateChooser();
		chooser2.setDateFormatString("dd/MM/yyyy");
		chooser2.addPropertyChangeListener(new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent e) {
				cal2 = chooser2.getCalendar();
			}
		});
		pannelloSup.add(new JLabel("Verifica Disponibilita' dal: "));
		pannelloSup.add(chooser1);
		pannelloSup.add(new JLabel(" al: "));
		pannelloSup.add(chooser2);
		numPersone=new JComboBox(persone);
		pannelloSup.add(new JLabel(" Posti:"));
		pannelloSup.add(numPersone);
		verifica=new JButton("Verifica");
		verifica.addActionListener(this);
		pannelloSup.add(verifica);
		mainPanel.add(pannelloSup,BorderLayout.NORTH);
		creaTabella();
		scroll=new JScrollPane(tabella);
		mainPanel.add(scroll,BorderLayout.CENTER);
		pannelloInf=new JPanel();
		prenota=new JButton("Prenota");
		prenota.addActionListener(this);
		pannelloInf.add(prenota);
		mainPanel.add(pannelloInf,BorderLayout.SOUTH);
		add(mainPanel);
		this.setVisible(true);
	}
	
	class MyDefaultTableModel extends DefaultTableModel {
		private static final long serialVersionUID = 1L;
		public boolean isCellEditable(int row, int column) {
			return false;
		}
	}
	
	private void creaTabella() {
		modelClienti = new MyDefaultTableModel();
		rs = g.database.querySelect("SELECT numero,numeroposti,prezzo from "+g.username+".camere");
		int columnCount = 3;
		modelClienti.addColumn("Camera");
		modelClienti.addColumn("Numero Posti");
		modelClienti.addColumn("Prezzo");
		Vector<String> row;
		while (rs.next()) {
			row = new Vector<String>(columnCount);
			for (int i = 1; i <= columnCount; i++) {
				row.add(rs.getString(i));
			}
			modelClienti.addRow(row);
		}
		tabella = new JTable(modelClienti);
		tabella.setGridColor(Color.LIGHT_GRAY);
		tabella.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==verifica){
			if(cal1==null) return;
			if(cal2==null) return;
			if(numPersone.getSelectedItem().equals("--")){
				cercaIndiff();
			}else{
				cercaConNumero();
			}
		}else if(e.getSource()==prenota){
			int row=tabella.getSelectedRow();
			if(row<0){
				JOptionPane.showMessageDialog(null,
						"Nessuna camera selezionata!", "Attenzione",
						JOptionPane.WARNING_MESSAGE, iconae);
				this.setVisible(true);
				return;
			}
			new FinestraPrenotazione((String) tabella.getValueAt(row, 0),g);
		}
	}

	private void cercaConNumero() {
		modelClienti.setRowCount(0);
		String data1=cal1.get(Calendar.YEAR)+"/"+cal1.get(Calendar.MONTH)+"/"+cal1.get(Calendar.DATE);
		String data2=cal2.get(Calendar.YEAR)+"/"+cal2.get(Calendar.MONTH)+"/"+cal2.get(Calendar.DATE);
		String query="SELECT numero,numeroposti,prezzo " +
				"from "+g.username+".Camere " +
				"where numeroposti>="+numPersone.getSelectedItem()+" AND numero not in (" +
				"Select camera " +
				"from "+g.username+".prenotazioni" +
				" where dal<="+data1+"<=al OR dal<="+data2+"<=al) and numero not in (select camera from associazione)";
		rs = g.database.cercaQuery(query);
		int columnCount = 3;
		Vector<String> row;
		while (rs.next()) {
			row = new Vector<String>(columnCount);
			for (int i = 1; i <= columnCount; i++) {
				row.add(rs.getString(i));
				}
			modelClienti.addRow(row);
		}
		tabella.setModel(modelClienti);
	}
	

	private void cercaIndiff() {
		modelClienti.setRowCount(0);
		String data1=cal1.get(Calendar.YEAR)+"/"+cal1.get(Calendar.MONTH)+"/"+cal1.get(Calendar.DATE);
		String data2=cal2.get(Calendar.YEAR)+"/"+cal2.get(Calendar.MONTH)+"/"+cal2.get(Calendar.DATE);
		String query="SELECT numero,numeroposti,prezzo " +
				"from "+g.username+".Camere " +
				"where numero not in (" +
				"Select camera " +
				"from "+g.username+".prenotazioni" +
				" where dal<="+data1+"<=al OR dal<="+data2+"<=al) and numero not in (select camera from associazione)";
		rs = g.getDB().cercaQuery(query);
		int columnCount = 3;
		Vector<String> row;
		while (rs.next()) {
			row = new Vector<String>(columnCount);
			for (int i = 1; i <= columnCount; i++) {
				row.add(rs.getString(i));
				}
			modelClienti.addRow(row);
		}
		tabella.setModel(modelClienti);
	}

}
